package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.service.ISysMenuService;
import com.itheima.ydd.service.ISysRoleMenuService;
import com.itheima.ydd.service.ISysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@Transactional
@RequestMapping("/sys_role")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysMenuService sysMenuService;
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @GetMapping("/allData")
    public ResultData getAllData() {
        return sysRoleService.getAllData();
    }


    /**
     * 分页查询
     *
     * @param page  页码数
     * @param limit 每页数量
     * @param name  角色名称，用于支持模糊查询
     * @return
     */
    @GetMapping("/list")
    public ResultData getList(Integer page, Integer limit, String name) {
        return sysRoleService.getList(page, limit, name);
    }

    /**
     * 回显
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id) {
        //通过id找到角色对象
        SysRole byId = sysRoleService.getById(id);

        //创建sysRoleDto对象，并将role对象的信息复制到sysRoleDto中
        SysRoleDto sysRoleDto = new SysRoleDto();
        BeanUtils.copyProperties(byId, sysRoleDto);
        //条件构造器
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        //在sys_role_menu表中匹配role_id
        lqw.eq(SysRoleMenu::getRoleId, byId.getId());
        //创建集合容器用于存储
        List<SysMenuDto> sysMenuDtos = new ArrayList<>();
        List<SysRoleMenu> list = sysRoleMenuService.list(lqw);
        SysMenuDto sysMenu = new SysMenuDto();
        for (SysRoleMenu sysRoleMenu : list) {
            SysMenu byId1 = sysMenuService.getById(Integer.parseInt(sysRoleMenu.getMenuId()));
            BeanUtils.copyProperties(byId1, sysMenu);

            sysMenuService.childList(sysMenu);
            sysMenuDtos.add(sysMenu);
        }
        sysRoleDto.setAuthList(sysMenuDtos);
        return ResultData.ok("查询成功", sysRoleDto);
    }

    /**
     * 添加角色
     *
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody SysRoleDto sysRoleDto) {
        return sysRoleService.add(sysRoleDto);
    }

    /**
     * 根据id删除角色
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(String id) {
        SysRole role = sysRoleService.getById(id);
        LambdaQueryWrapper<SysRoleMenu> qw = new LambdaQueryWrapper<>();
        qw.eq(SysRoleMenu::getRoleId, role.getId());
        sysRoleMenuService.remove(qw);
        sysRoleService.removeById(id);
        return ResultData.ok("删除成功", null);
    }

    /**
     * 修改角色信息
     *
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody SysRoleDto sysRoleDto) {
        //将sysRoleDto拷贝到sysRoleDto
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDto, sysRole);

        //获取角色所拥有的的菜单id
        List<String> menuids = sysRoleDto.getMenuids();

        //得到要修改角色的id值
        String roleId = sysRoleDto.getId();
        //条件构造器，根据角色id进行查询
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysRoleMenu::getRoleId, roleId);
        //将原来信息进行移除
        sysRoleMenuService.remove(lqw);
        //遍历菜单id，进行存储
        for (String menuid : menuids) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuid);
            sysRoleMenuService.save(sysRoleMenu);
        }
        return ResultData.ok("修改成功", null);
    }
}
